#!/usr/bin/python
# -*- coding: utf-8 -*-
#coding=utf-8
# ### create by FlyShine 	###
# ### 2011年6月7日 			###
# ### version: V0.1 		###
import sys
import os
import codecs #用于写txt文档时编码UTF-8
import re #正则表达式
import string #用到其中的atoi()
from xml.sax import make_parser, SAXException
from xml.sax.handler import ContentHandler
def_mid_result_fn="/tmp/xml-mid.txt"
xml_fn=""
hanle_midre_sh=""
strip_prefix=""
vers_no_fn=""
all_chng_fn=""
vers_info_fn=""

class SvnlogParse(ContentHandler): #定义一个handler类
	def __init__(self,mid_result_fn,hanle_midre_sh,strip_prefix,vers_no_fn,vers_info_fn,all_chng_fn):
		self.str_chglist='';
		self.str_verinfo='';
		self.str_all_chng='';
		self.str_prefix=strip_prefix;
		self.midre_sh=hanle_midre_sh;
		self.mid_result_fn = mid_result_fn;
		self.mid_result_file='';
		self.current_name='';
		self.current_action='';
		self.current_version='';
		self.vers_info_fn = vers_info_fn;
		self.vers_no_file=open(vers_no_fn,'a');
		self.all_chng_file=open(all_chng_fn,'a');

	def format_result(self,item ,str):
		self.str_chglist +="%s\n"%(str);
	def write_result(self):
		self.result_file.write(u"%s"%self.str_chglist);
	def startElement(self,name,attrs):
		self.current_name =name;
		if name == "logentry":
			self.current_version=attrs.get("revision");
			self.mid_result_file=open(self.mid_result_fn,'w');#进入到一条新的记录时打开文件
			self.vers_no_file.write(u"%s\n"%self.current_version);
		if name == "path":
			self.current_action=attrs.get("action");
				
	def endElement(self,name):
		if name == "logentry":
			self.mid_result_file.write(u"%s"%self.str_verinfo);#写入当前版本待合并的信息
			self.mid_result_file.close();
			self.str_verinfo='';
			# command “处理使用的脚本 待处理的文件 前缀 版本号 接收写入的文件”
			command ='%s %s %s %s %s'%(self.midre_sh,self.mid_result_fn,self.str_prefix,self.current_version,self.vers_info_fn)
			os.system(command);
		if name == "log":
			self.all_chng_file.write(u"%s"%self.str_all_chng);
			self.all_chng_file.close();
			self.vers_no_file.close();
			raise SAXException('endElement:log:Finished search...') # stop parsing
	def characters(self,chars):
		if ( string.strip(chars)=="" ) or ( chars.find(self.str_prefix) == -1 ):
			return;
		name = self.current_name;
		if name == "path":
			self.format_result(name, chars);
			self.str_all_chng +=u"%s\n"%chars;
			if self.current_action == "M":
				self.str_verinfo +=u"%s\n"%(chars);
			else:
				self.str_verinfo +=u"%s\n"%(os.path.dirname(chars));
		  

def search_log_paths(xmlfile,mid_result_fn,hanle_midre_sh,strip_prefix,vers_no_fn,vers_info_fn,all_chng_fn):
	handler = SvnlogParse(mid_result_fn,hanle_midre_sh,strip_prefix,vers_no_fn,vers_info_fn,all_chng_fn);
	parser = make_parser();
	parser.setContentHandler(handler);
	try:
		parser.parse(open(xmlfile));
	except SAXException:
		#print "finished parse xml,see result";
		return None

if __name__ == '__main__':
    import sys
    if (len(sys.argv)< 7):
		print "argv length %s"%(len(sys.argv));
		print "MISS argv, please give the name of svn_log_xmlfile and result_file_name ...";
    else:
        xml_fn = "%s"%sys.argv[1];#xml_file_name;
        hanle_midre_sh= "%s"%sys.argv[2];#处理待合并文件、目录的脚本;
        strip_prefix= "%s"%sys.argv[3];#待删除的前缀;
        vers_no_fn= "%s"%sys.argv[4];#所有版本号存放的文件和文件夹;
        vers_info_fn= "%s"%sys.argv[5];#每个版本修改待合并的文件和目录;
        all_chng_fn= "%s"%sys.argv[6];#所有版本修改的文件和目录;
        search_log_paths(xml_fn,def_mid_result_fn,hanle_midre_sh,strip_prefix,vers_no_fn,vers_info_fn,all_chng_fn);
